﻿using Motions;
using Station;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Tool
{
    class SystemCfg
    {

        private static string IOFile = AppDomain.CurrentDomain.BaseDirectory + "\\" + "Config" + "\\" + "IOCfg.xml";//路径

        private static string AxisFile = AppDomain.CurrentDomain.BaseDirectory + "\\" + "Config" + "\\" + "AxisCfg.xml";//路径

        private static string ParamFile = AppDomain.CurrentDomain.BaseDirectory + "\\" + "Config" + "\\" + "Param.xml";//路径

        #region 板卡IO输入输出

        /// <summary>
        /// 板卡输入输出
        /// </summary>
        public struct CardInfo
        {
            /// <summary>
            /// 名称
            /// </summary>
            public string Name;
            /// <summary>
            /// 卡号
            /// </summary>
            public int CardN0;
            /// <summary>
            /// 线号
            /// </summary>
            public string WizeNo;
            /// <summary>
            /// 序列号
            /// </summary>
            public int Index;
            /// <summary>
            /// 序列号
            /// </summary>
            public int Axisfg;
        }

        /// <summary>
        /// 输入
        /// </summary>
        public static Dictionary<string, CardInfo> In = new Dictionary<string, CardInfo>();//轴参数  要显示到DatagridView

        /// <summary>
        /// 输出
        /// </summary>
        public static Dictionary<string, CardInfo> Out = new Dictionary<string, CardInfo>();//轴参数 要显示到DatagridView

        /// <summary>
        /// 板卡输入输出写进内存中
        /// </summary>
        /// <param name="Node">根节点</param>
        public static void WriteCardXmlToSystem(Dictionary<string, CardInfo> Info, string Node)
        {
            XmlDocument xmldoc = new XmlDocument();//实例化
            try
            {
                Info.Clear();//字典清零
                xmldoc.Load(IOFile);//加载XML文件
                if (xmldoc.HasChildNodes)
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg" + "/" + Node);//根节点
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        foreach (XmlNode childnode in nodelist)
                        {
                            CardInfo p = new CardInfo();
                            XmlElement element = (XmlElement)childnode;
                            string name = element.GetAttribute("Name");
                            p.Name = element.GetAttribute("Name");
                            p.CardN0 = int.Parse(element.GetAttribute("CardNo"));
                            p.WizeNo = element.GetAttribute("WizeNo");
                            p.Index = int.Parse(element.GetAttribute("Index"));
                            p.Axisfg = int.Parse(element.GetAttribute("Axisfg"));
                            Info.Add(name, p);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Node);
            }
        }

        /// <summary>
        /// 板卡输入输出写进XML文件中
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void WriteCardDgvToXml(Dictionary<string, CardInfo> Info, DataGridView dgv, string Node)
        {
            XmlDocument xmldoc = new XmlDocument();//实例化一个xml文件类
            try
            {
                //先删除指定节点
                xmldoc.Load(IOFile);//从xPath路径中加载一个xml文件
                XmlNode xn = xmldoc.SelectSingleNode("SystemCfg" + "/" + Node);//查找匹配第一个xml节点
                xn.RemoveAll();//删除根节点下子节点所有数据

                //插入节点值
                foreach (DataGridViewRow pair in dgv.Rows)//再加上
                {
                    XmlElement xe = xmldoc.CreateElement(Node);
                    xe.SetAttribute("Name", pair.Cells[0].Value.ToString());
                    xe.SetAttribute("CardNo", pair.Cells[1].Value.ToString());
                    xe.SetAttribute("WizeNo", pair.Cells[2].Value.ToString());
                    xe.SetAttribute("Index", pair.Cells[3].Value.ToString());
                    xn.AppendChild(xe);
                }
                xmldoc.Save(IOFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Node);
            }


        }

        /// <summary>
        /// XML文件中输入输出读取到DGV表格
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void ReadCardXmlToDGV(DataGridView dgv, string Node)
        {
            dgv.Rows.Clear();//清除集合
            XmlDocument xmldoc = new XmlDocument();//实例化  
            try
            {
                xmldoc.Load(IOFile);//加载XML文档   
                if (xmldoc.HasChildNodes)//XML是否有节点
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg" + "/" + Node);//获取SystemCfg节点下PointCfg节点集合
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        int num = 0;
                        foreach (XmlNode childnode in nodelist)
                        {
                            int num2 = 0;
                            dgv.Rows.Add();
                            XmlElement element = (XmlElement)childnode;
                            int num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("CardNo");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("WizeNo");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Index");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Name");

                            //最后加上的
                            num3 = num;
                            num = num3 + 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Node);
            }
        }


        #endregion

        #region 轴参数点位参数

        /// <summary>
        /// 轴名称
        /// </summary>
        public struct AxisNameInfo
        {
            /// <summary>
            /// 名称
            /// </summary>
            public string Name;
            /// <summary>
            /// 卡号
            /// </summary>
            public int CardN0;
            /// <summary>
            /// 序列号
            /// </summary>
            public int Index;
        }

        /// <summary>
        /// 轴参数
        /// </summary>
        public struct AxisInfo
        {
            /// <summary>
            /// 轴号
            /// </summary>
            public int AxisNo;
            /// <summary>
            /// 轴名称
            /// </summary>
            public string AxisName;
            /// <summary>
            /// 脉冲换算比 (每转脉冲数)/(丝杆导乘)*(减速比)  一个脉冲等于0.001mm  等于1um 
            /// </summary>
            public double PluseRatio;
            /// <summary>
            /// 加速时间
            /// </summary>
            public double Acc;
            /// <summary>
            /// 减速时间
            /// </summary>
            public double Dec;
            /// <summary>
            /// 起跳速度
            /// </summary>
            public double StartSpeed;
            /// <summary>
            /// 平滑时间
            /// </summary>
            public double SmoothTime;
            /// <summary>
            /// 回原高速
            /// </summary>
            public double OrgHSpeed;
            /// <summary>
            /// 回原方向
            /// </summary>
            public int HomeDir;
            /// <summary>
            /// 回原低速
            /// </summary>
            public double OrgLSpeed;
            /// <summary>
            /// 回原方式
            /// </summary>
            public uint HomeMode;
            /// <summary>
            /// 偏移距离
            /// </summary>
            public double CrossDistance;
        }

        /// <summary>
        /// 轴参数
        /// </summary>
        public static Dictionary<int, AxisInfo> AxisCfg = new Dictionary<int, AxisInfo>();//轴参数 要显示到DatagridView

        /// <summary>
        /// 轴名称
        /// </summary>
        public static Dictionary<string, AxisNameInfo> AxisName = new Dictionary<string, AxisNameInfo>();//轴参数 不需要显示到DatagridView 但是要写进系统中

      
        /// <summary>
        /// 轴名称写进内存
        /// </summary>
        /// <param name="Node">根节点</param>
        public static void WriteAxisNameXmlToSystem(Dictionary<string, AxisNameInfo> Info, string Node)
        {
            XmlDocument xmldoc = new XmlDocument();//实例化
            try
            {
                Info.Clear();//字典清零
                xmldoc.Load(AxisFile);//加载XML文件
                if (xmldoc.HasChildNodes)
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg" + "/" + Node);//根节点
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        foreach (XmlNode childnode in nodelist)
                        {
                            AxisNameInfo p = new AxisNameInfo();
                            XmlElement element = (XmlElement)childnode;
                            string name = element.GetAttribute("Name");
                            p.Name = element.GetAttribute("Name");
                            p.CardN0 = int.Parse(element.GetAttribute("CardNo"));
                            p.Index = int.Parse(element.GetAttribute("Index"));
                            Info.Add(name, p);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Node);
            }
        }

        /// <summary>
        /// 轴名称读取到DGV表格
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void ReadAxisNameXmlToDGV(DataGridView dgv, string Node)
        {
            dgv.Rows.Clear();//清除集合
            XmlDocument xmldoc = new XmlDocument();//实例化  
            try
            {
                xmldoc.Load(AxisFile);//加载XML文档   
                if (xmldoc.HasChildNodes)//XML是否有节点
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg" + "/" + Node);//获取SystemCfg节点下PointCfg节点集合
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        int num = 0;
                        foreach (XmlNode childnode in nodelist)
                        {
                            int num2 = 0;
                            dgv.Rows.Add();
                            XmlElement element = (XmlElement)childnode;
                            int num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("CardNo");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Index");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Name");

                            //最后加上的
                            num3 = num;
                            num = num3 + 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Node);
            }
        }

        /// <summary>
        /// 轴参数写进内存
        /// </summary>
        /// <param name="Node">根节点</param>
        public static void WriteAxisCfgXmlToSystem()
        {
            XmlDocument xmldoc = new XmlDocument();//实例化
            try
            {
                AxisCfg.Clear();//字典清零
                xmldoc.Load(AxisFile);//加载XML文件
                if (xmldoc.HasChildNodes)
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg/AxisCfg");//根节点
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        foreach (XmlNode childnode in nodelist)
                        {
                            AxisInfo p = new AxisInfo();
                            XmlElement element = (XmlElement)childnode;
                            int index = int.Parse(element.GetAttribute("AxisNo"));
                            p.AxisNo = int.Parse(element.GetAttribute("AxisNo"));
                            p.AxisName = element.GetAttribute("AxisName");
                            p.PluseRatio = double.Parse(element.GetAttribute("PluseRatio"));
                            p.Acc = Convert.ToDouble(element.GetAttribute("Acc"));
                            p.Dec = Convert.ToDouble(element.GetAttribute("Dec"));
                            p.StartSpeed = Convert.ToDouble(element.GetAttribute("StartSpeed"));
                            p.SmoothTime = Convert.ToDouble(element.GetAttribute("SmoothTime"));
                            p.OrgHSpeed = Convert.ToDouble(element.GetAttribute("OrgHSpeed"));
                            p.HomeDir = int.Parse(element.GetAttribute("HomeDir"));
                            p.OrgLSpeed = Convert.ToDouble(element.GetAttribute("OrgLSpeed"));
                            p.HomeMode = Convert.ToUInt32(element.GetAttribute("HomeMode"));
                            p.CrossDistance = Convert.ToDouble(element.GetAttribute("CrossDistance"));
                            AxisCfg.Add(index, p);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 轴参数写进XML文件
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void WriteAxisCfgDgvToXml(DataGridView dgv)
        {
            XmlDocument xmldoc = new XmlDocument();//实例化一个xml文件类
            try
            {
                //先删除指定节点
                xmldoc.Load(AxisFile);//从xPath路径中加载一个xml文件
                XmlNode xn = xmldoc.SelectSingleNode("SystemCfg/AxisCfg");//查找匹配第一个xml节点
                xn.RemoveAll();//删除根节点下子节点所有数据

                //插入节点值
                foreach (DataGridViewRow pair in dgv.Rows)//再加上
                {
                    XmlElement xe = xmldoc.CreateElement("AxisCfg");
                    xe.SetAttribute("AxisNo", pair.Cells[0].Value.ToString());
                    xe.SetAttribute("AxisName", pair.Cells[1].Value.ToString());
                    xe.SetAttribute("PluseRatio", pair.Cells[2].Value.ToString());
                    xe.SetAttribute("Acc", pair.Cells[3].Value.ToString());
                    xe.SetAttribute("Dec", pair.Cells[4].Value.ToString());
                    xe.SetAttribute("StartSpeed", pair.Cells[5].Value.ToString());
                    xe.SetAttribute("SmoothTime", pair.Cells[6].Value.ToString());
                    xe.SetAttribute("OrgHSpeed", pair.Cells[7].Value.ToString());
                    xe.SetAttribute("HomeDir", pair.Cells[8].Value.ToString());
                    xe.SetAttribute("OrgLSpeed", pair.Cells[9].Value.ToString());
                    xe.SetAttribute("HomeMode", pair.Cells[10].Value.ToString());
                    xe.SetAttribute("CrossDistance", pair.Cells[11].Value.ToString());
                    xn.AppendChild(xe);
                }
                xmldoc.Save(AxisFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        /// <summary>
        /// XML文件中轴参数读取到DGV表格
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void ReadAxisCfgXmlToDGV(DataGridView dgv)
        {
            dgv.Rows.Clear();//清除集合
            XmlDocument xmldoc = new XmlDocument();//实例化  
            try
            {
                xmldoc.Load(AxisFile);//加载XML文档   
                if (xmldoc.HasChildNodes)//XML是否有节点
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg/AxisCfg");//获取SystemCfg节点下PointCfg节点集合
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        int num = 0;
                        foreach (XmlNode childnode in nodelist)
                        {
                            int num2 = 0;
                            dgv.Rows.Add();
                            XmlElement element = (XmlElement)childnode;
                            int num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("AxisNo");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("AxisName");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("PluseRatio");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Acc");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Dec");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("StartSpeed");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("SmoothTime");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("OrgHSpeed");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("HomeDir");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("OrgLSpeed");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("HomeMode");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("CrossDistance");
                            //最后加上的
                            num3 = num;
                            num = num3 + 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region 系统参数

        /// <summary>
        /// 系统参数
        /// </summary>
        public struct ParamInfo
        {
            /// <summary>
            /// 名称
            /// </summary>
            public string Name;
            /// <summary>
            /// 描述
            /// </summary>
            public string Exp;
            /// <summary>
            /// 当前值
            /// </summary>
            public string Value;
        }

        /// <summary>
        /// 系统参数
        /// </summary>
        public static Dictionary<string, ParamInfo> Param = new Dictionary<string, ParamInfo>();//轴参数  要显示到DatagridView

        /// <summary>
        /// 点位参数写进内存
        /// </summary>
        /// <param name="Node">根节点</param>
        public static void WriteParamXmlToSystem()
        {
            XmlDocument xmldoc = new XmlDocument();//实例化
            try
            {
                Param.Clear();//字典清零
                xmldoc.Load(ParamFile);//加载XML文件
                if (xmldoc.HasChildNodes)
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg/Param");//根节点
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        foreach (XmlNode childnode in nodelist)
                        {
                            ParamInfo p = new ParamInfo();
                            XmlElement element = (XmlElement)childnode;
                            string name = element.GetAttribute("Name");
                            p.Name = element.GetAttribute("Name");
                            p.Exp = element.GetAttribute("Exp");
                            p.Value = element.GetAttribute("Value");
                            Param.Add(name, p);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 点位参数写到XML文件
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void WriteParamDgvToXml(DataGridView dgv)
        {
            XmlDocument xmldoc = new XmlDocument();//实例化一个xml文件类
            try
            {
                //先删除指定节点
                xmldoc.Load(ParamFile);//从xPath路径中加载一个xml文件
                XmlNode xn = xmldoc.SelectSingleNode("SystemCfg/Param");//查找匹配第一个xml节点
                xn.RemoveAll();//删除根节点下子节点所有数据

                //插入节点值
                foreach (DataGridViewRow pair in dgv.Rows)//再加上
                {
                    XmlElement xe = xmldoc.CreateElement("Param");
                    xe.SetAttribute("Name", pair.Cells[0].Value.ToString());
                    xe.SetAttribute("Exp", pair.Cells[1].Value.ToString());
                    xe.SetAttribute("Value", pair.Cells[2].Value.ToString());                   
                    xn.AppendChild(xe);
                }
                xmldoc.Save(ParamFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        /// <summary>
        /// XML文件中点位参数读取到DGV表格
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void ReadParamXmlToDGV(DataGridView dgv)
        {
            dgv.Rows.Clear();//清除集合
            XmlDocument xmldoc = new XmlDocument();//实例化  
            try
            {
                xmldoc.Load(ParamFile);//加载XML文档   
                if (xmldoc.HasChildNodes)//XML是否有节点
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg/Param");//获取SystemCfg节点下PointCfg节点集合
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        int num = 0;
                        foreach (XmlNode childnode in nodelist)
                        {
                            int num2 = 0;
                            dgv.Rows.Add();
                            XmlElement element = (XmlElement)childnode;
                            int num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Name");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Exp");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Value");
                            num3 = num2;
                            num2 = num3 + 1;
                                                  

                            //最后加上的
                            num3 = num;
                            num = num3 + 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



        #endregion

        #region 站位配置

        public static List<StationBase> listStation = new List<StationBase>();

        public static void AddStation()
        {
            try
            {
                UpAndDownLoad upAndDownLoad = new UpAndDownLoad("UpAndDownLoad");
                MagSplit magSplit = new MagSplit("MagSplit");
                SlidesTransit SlidesTransit = new SlidesTransit("SlidesTransit");
                TransitCarryLocation TransitCarryLocation = new TransitCarryLocation("TransitCarryLocation");
                TransitPickUp TransitPickUp = new TransitPickUp("TransitPickUp");
                FlyFilming1 flyFilming1 = new FlyFilming1("FlyFilming1");
                TransitCarry TransitCarry = new TransitCarry("TransitCarry");
                FlyFilming2 flyFilming2 = new FlyFilming2("FlyFilming2");
                FlyFilming3 flyFilming3 = new FlyFilming3("FlyFilming3");
                FlyAfterPickUp flyAfterPickUp = new FlyAfterPickUp("FlyAfterPickUp");
                PutDownOKNG putDownOKNG = new PutDownOKNG("PutDownOKNG");
                OKFull oKFull = new OKFull("OKFull");
                OKMagMerge oKMagMerge = new OKMagMerge("OKMagMerge");

                listStation.Add(upAndDownLoad);
                listStation.Add(magSplit);
                listStation.Add(SlidesTransit);
                listStation.Add(TransitCarryLocation);
                listStation.Add(TransitPickUp);
                listStation.Add(flyFilming1);
                listStation.Add(TransitCarry);
                listStation.Add(flyFilming2);
                listStation.Add(flyFilming3);
                listStation.Add(flyAfterPickUp);
                listStation.Add(putDownOKNG);
                listStation.Add(oKFull);
                listStation.Add(oKMagMerge);
            }
            catch (Exception ex)
            {

                throw ex;
            }  
        }
        #endregion

        #region 轴点位参数

       

        /// <summary>
        /// XML文件点位信息
        /// </summary>
        public struct PosInfo
        {
            /// <summary>
            /// 点位名称
            /// </summary>
            public string PointName;
            /// <summary>
            /// 轴名称
            /// </summary>
            public string AxisName;
            /// <summary>
            /// 轴号
            /// </summary>
            public int AxisNo; 
            /// <summary>
            /// 坐标值
            /// </summary>
            public double Point;
            /// <summary>
            /// 速度
            /// </summary>
            public double Speed;
            ///
            //public double Acc;//加速时间
            //public double Dec;//减速时间
            //public double StartSpeed;//起跳速度
            //public double SmoothTime;//平滑时间

        }
      
        public static Dictionary<string, PosInfo> UpAndDownLoad = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> MagSplit = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> SlidesTransit = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> TransitCarryLocation = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> TransitPickUp = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> FlyFilming1 = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> TransitCarry = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> FlyFilming2 = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> FlyFilming3 = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> FlyAfterPickUp = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> PutDownOKNG = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> OKFull = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView
        public static Dictionary<string, PosInfo> OKMagMerge = new Dictionary<string, PosInfo>();//轴参数 要显示到DatagridView


        /// <summary>
        /// 点位参数写进内存
        /// </summary>
        /// <param name="Node">根节点</param>
        public static void WritePointXmlToSystem(Dictionary<string, PosInfo> DicPoint,String SectionName)
        {
            XmlDocument xmldoc = new XmlDocument();//实例化
            try
            {
                DicPoint.Clear();//字典清零
                xmldoc.Load(AxisFile);//加载XML文件
                if (xmldoc.HasChildNodes)
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg"+"/" + SectionName);//根节点
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        foreach (XmlNode childnode in nodelist)
                        {
                            PosInfo p = new PosInfo();
                            XmlElement element = (XmlElement)childnode;
                            string name = element.GetAttribute("PointName");
                            p.PointName = element.GetAttribute("PointName");
                            p.AxisName = element.GetAttribute("AxisName");
                            p.AxisNo = int.Parse(element.GetAttribute("AxisNo"));
                            p.Point = Convert.ToDouble(element.GetAttribute("Point"));
                            p.Speed = Convert.ToDouble(element.GetAttribute("Speed"));
                            //p.Acc = Convert.ToDouble(element.GetAttribute("Acc"));
                            //p.Dec = Convert.ToDouble(element.GetAttribute("Dec"));
                            //p.StartSpeed = Convert.ToDouble(element.GetAttribute("StartSpeed"));
                            //p.SmoothTime = Convert.ToDouble(element.GetAttribute("SmoothTime"));

                            DicPoint.Add(name, p);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 点位参数写到XML文件
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void WritePointDgvToXml(DataGridView dgv,string SectionName)
        {
            XmlDocument xmldoc = new XmlDocument();//实例化一个xml文件类
            try
            {
                //先删除指定节点
                xmldoc.Load(AxisFile);//从xPath路径中加载一个xml文件
                XmlNode xn = xmldoc.SelectSingleNode("SystemCfg" +"/" + SectionName);//查找匹配第一个xml节点
                xn.RemoveAll();//删除根节点下子节点所有数据

                //插入节点值
                foreach (DataGridViewRow pair in dgv.Rows)//再加上
                {
                    XmlElement xe = xmldoc.CreateElement("PointCfg");
                    xe.SetAttribute("PointName", pair.Cells[0].Value.ToString());
                    xe.SetAttribute("AxisName", pair.Cells[1].Value.ToString());
                    xe.SetAttribute("AxisNo", pair.Cells[2].Value.ToString());
                    xe.SetAttribute("Point", pair.Cells[3].Value.ToString());
                    xe.SetAttribute("Speed", pair.Cells[4].Value.ToString());
                    //xe.SetAttribute("Acc", pair.Cells[5].Value.ToString());
                    //xe.SetAttribute("Dec", pair.Cells[6].Value.ToString());
                    //xe.SetAttribute("StartSpeed", pair.Cells[7].Value.ToString());
                    //xe.SetAttribute("SmoothTime", pair.Cells[8].Value.ToString());

                    xn.AppendChild(xe);
                }
                xmldoc.Save(AxisFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        /// <summary>
        /// XML文件中点位参数读取到DGV表格
        /// </summary>
        /// <param name="dgvPoint">表格名称</param>
        /// <param name="name">二级节点 轴名称</param>
        public static void ReadPointXmlToDGV(DataGridView dgv, string SectionName)
        {
            dgv.Rows.Clear();//清除集合
            XmlDocument xmldoc = new XmlDocument();//实例化  
            try
            {
                xmldoc.Load(AxisFile);//加载XML文档   
                if (xmldoc.HasChildNodes)//XML是否有节点
                {
                    //获取根节点下的指定子节点的所有集合
                    XmlNodeList nodelist = xmldoc.SelectNodes("SystemCfg"+ "/" + SectionName);//获取SystemCfg节点下PointCfg节点集合
                    if (nodelist.Count > 0)
                    {
                        nodelist = nodelist.Item(0).ChildNodes;
                        int num = 0;
                        foreach (XmlNode childnode in nodelist)
                        {
                            int num2 = 0;
                            dgv.Rows.Add();
                            XmlElement element = (XmlElement)childnode;
                            int num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("PointName");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("AxisName");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("AxisNo");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Point");
                            num3 = num2;
                            num2 = num3 + 1;
                            dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Speed");

                            //num3 = num2;
                            //num2 = num3 + 1;
                            //dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Acc");
                            //num3 = num2;
                            //num2 = num3 + 1;
                            //dgv.Rows[num].Cells[num3].Value = element.GetAttribute("Dec");
                            //num3 = num2;
                            //num2 = num3 + 1;
                            //dgv.Rows[num].Cells[num3].Value = element.GetAttribute("StartSpeed");
                            //num3 = num2;
                            //num2 = num3 + 1;
                            //dgv.Rows[num].Cells[num3].Value = element.GetAttribute("SmoothTime");

                            //最后加上的
                            num3 = num;
                            num = num3 + 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 把点位名称进行一个选择
        /// </summary>
        /// <param name="SectionName"></param>
        /// <returns></returns>
        public static Dictionary<string, SystemCfg.PosInfo> Po(string SectionName)
        {
 
        Dictionary<string, SystemCfg.PosInfo> list = new Dictionary<string, SystemCfg.PosInfo>();

            switch (SectionName)
            {
                case "UpAndDownLoad":
                  list = UpAndDownLoad;
                    break;
                case "MagSplit":
                    list = MagSplit;
                    break;
                case "SlidesTransit":
                    list = SlidesTransit;
                    break;
             case "TransitCarryLocation":
                  list = TransitCarryLocation;
                    break;
                case "TransitPickUp":
                    list = TransitPickUp;
                    break;
                case "FlyFilming1":
                    list = FlyFilming1;
                    break;
             case "TransitCarry":
                  list = TransitCarry;
                    break;
                case "FlyFilming2":
                    list = FlyFilming2;
                    break;
                case "FlyFilming3":
                    list = FlyFilming3;
                    break;
             case "FlyAfterPickUp":
                  list = FlyAfterPickUp;
                    break;
                case "PutDownOKNG":
                    list = PutDownOKNG;
                    break;
                case "OKFull":
                    list = OKFull;
                    break;
             case "OKMagMerge":
                    list = OKMagMerge;
                    break;
                default:
                    MessageBox.Show("无XML表格文件");
                    break;
            }
            return list;

        }

        #endregion

    }
}


